angular.module('App').run(['Config', '$state', '$q', '$timeout', '$location', '$rootScope','$cookies','Action','$http',
    function (Config, $state, $q, $timeout, $location, $rootScope,$cookies,Action,$http) {
        var RT = {},
            defaultUrl = 'app.index';
        RT.menu = Config['menu'];

        $rootScope.RT = RT;


        function f() {
            Action.ajax({
                url: 'http://newbusiness.speeda.cn/platform/index/index/a/1/b/2',
                type: 'post',
                params: {
                    c: 222222
                },
                fn: function (data) {
                    console.log(data);
                }
            });
        }
        f();

        if ('WebSocket' in window) {
            var scoket = new WebSocket('ws://114.55.60.173:9527');
            scoket.onopen = function () {
                scoket.send([111111,222]);
            };
            scoket.onmessage = function (event) {
                console.log(event.data);
            }
            // scoket.onclose = function () { };
            // scoket.onerror = function () { };
        }



        // --------
        $state.prev = {};
        $rootScope.$on('$locationChangeSuccess', function (event) {
            if ($location.otherwise) {
                event.preventDefault();
                delete $location.otherwise;
                $state.transitionTo(defaultUrl);
            }
            $timeout(function () {
                Action.wrapper();
            }, 500);
        });

        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            $state.prev.state = fromState.name;
            $state.prev.params = fromParams;

            var cookie = $cookies.get('app');
            // if (!cookie && toState.name !== 'login') {
            //     event.preventDefault();
            //     localStorage.removeItem('app');
            //     $cookies.remove('app');
            //     $state.transitionTo('login');
            // } else if (toState.name === 'app') {
            //     event.preventDefault();
            //     $state.transitionTo(defaultUrl);
            // }
        });


    }]);